import requests

baseUrl = 'http://192.168.43.39:5000'

def get(api:str):
    error = None
    result = None
    # 如果api不以 / 开头 则为其添加 /
    if(api.startswith('/')):
        url = baseUrl + api
    else:
        url = baseUrl + '/' + api
    # 发起请求 处理返回结果
    try:
        result = requests.get(url).json()  
        if(result['message'] and result['message'].find('404')>=0):
            error = '请检查url请求地址是否正确'
    except Exception as e:
        if('NewConnectionError'.find(e.__str__())>=0):
            print(e)
            error = '请检查url在局域网内容是否可访问' 
        else:
            error = e.__str__() 

    if(error):
        return {
            'message': error,
            'status': False
        }
    else: 
        return result

def post(api:str, data):
    error = None
    result = None
    # 如果api不以 / 开头 则为其添加 /
    if(api.startswith('/')):
        url = baseUrl + api
    else:
        url = baseUrl + '/' + api
    # 发起请求 处理返回结果
    try:
        result = requests.post(url, data).json()  
        if(result['message'] and result['message'].find('404')>=0):
            error = '请检查url请求地址是否正确'
    except Exception as e:
        if('NewConnectionError'.find(e.__str__())>=0):
            print(e)
            error = '请检查url在局域网内容是否可访问' 
        else:
            error = e.__str__() 

    if(error):
        return {
            'message': error,
            'status': False
        }
    else: 
        return result
